package com.the4man.cnbdw.utils;

import com.the4man.framework.model.Employee;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Created by lenovo on 2017/12/12.
 */
public class Tools {
    public Tools() {
    }

    public static List<Employee> readEmployeeByExcel(File file) {
        List<Employee> list = new ArrayList();
        Workbook wb = null;

        try {
            wb = Workbook.getWorkbook(file);
        } catch (BiffException var11) {
            var11.printStackTrace();
        } catch (IOException var12) {
            var12.printStackTrace();
        }

        if(wb == null) {
            return null;
        } else {
            Sheet[] sheet = wb.getSheets();
            if(sheet != null && sheet.length > 0) {
                int rowNum = sheet[0].getRows();

                for(int j = 0; j < rowNum; ++j) {
                    if(j >= 1) {
                        Cell[] cells = sheet[0].getRow(j);
                        if(cells != null && cells.length > 0) {
                            Employee ip = new Employee();
                            if("".equals(cells[1].getContents())) {
                                break;
                            }

                            ip.setPhoto(cells[0].getContents());
                            ip.setName(cells[1].getContents());
                            ip.setGender(Short.valueOf("男".equals(cells[2].getContents())?"1":"0"));
                            ip.setCompany(cells[3].getContents());
                            String area = "68e91ac7-7aca-456d-a0de-1c57d0d813cf";
                            if("常住人员区域".equals(cells[4].getContents())) {
                                ip.setRegionId(area);
                            } else if("公用客房区域".equals(cells[4].getContents())) {
                                area = "6ebf01dd-c913-45b4-92c1-c5f134f52ae1";
                                ip.setRegionId(area);
                            }

                            ip.setCardType(Short.valueOf("身份证".equals(cells[5].getContents())?"1":"2"));
                            ip.setCardNo(cells[6].getContents());
                            String value = cells[7].getContents();
                            String lev = "2";
                            if("舰员".equals(value)) {
                                lev = "1";
                                ip.setLevel(lev);
                            } else if("搭乘人员".equals(value)) {
                                lev = "2";
                                ip.setLevel(lev);
                            }

                            ip.setRecordPosition(Short.valueOf("是".equals(cells[8].getContents())?"1":"0"));
                            list.add(ip);
                        }
                    }
                }
            }

            wb.close();
            return list;
        }
    }

    public static boolean upZipFile(File zipFile, String folderPath) {
        try {
            ZipFile zfile = new ZipFile(zipFile);
            Enumeration<?> zList = zfile.entries();
            ZipEntry ze = null;
            byte[] buf = new byte[1024];
            int var6 = 0;

            while(true) {
                while(zList.hasMoreElements()) {
                    ze = (ZipEntry)zList.nextElement();
                    if(ze.isDirectory()) {
                        System.out.println("upZipFile ze.getName() = " + ze.getName());
                        String dirstr = folderPath + ze.getName();
                        dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
                        System.out.println("upZipFile str = " + dirstr);
                        File f = new File(dirstr);
                        f.mkdirs();
                    } else {
                        System.out.println("upZipFile ze.getName() = " + ze.getName());
                        OutputStream os = new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())));
                        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
                        boolean var9 = false;

                        int readLen;
                        while((readLen = is.read(buf, 0, 1024)) != -1) {
                            os.write(buf, 0, readLen);
                        }

                        is.close();
                        os.close();
                        ++var6;
                    }
                }

                zfile.close();
                System.out.println("upZipFile finish");
                zipFile.delete();
                return true;
            }
        } catch (Exception var10) {
            var10.printStackTrace();
            return false;
        }
    }

    private static File getRealFileName(String baseDir, String absFileName) {
        String[] dirs = absFileName.split("/");
        File ret = new File(baseDir);
        String substr = null;
        if(dirs.length > 1) {
            for(int i = 0; i < dirs.length - 1; ++i) {
                substr = dirs[i];

                try {
                    substr = new String(substr.getBytes("8859_1"), "GB2312");
                } catch (UnsupportedEncodingException var8) {
                    var8.printStackTrace();
                }

                ret = new File(ret, substr);
            }

            System.out.println("upZipFile 1ret = " + ret);
            if(!ret.exists()) {
                ret.mkdirs();
            }

            substr = dirs[dirs.length - 1];

            try {
                substr = new String(substr.getBytes("8859_1"), "GB2312");
                System.out.println("upZipFile substr = " + substr);
            } catch (UnsupportedEncodingException var7) {
                var7.printStackTrace();
            }

            ret = new File(ret, substr);
            System.out.println("upZipFile 2ret = " + ret);
            return ret;
        } else {
            if(!baseDir.endsWith("/") && !absFileName.startsWith("/")) {
                ret = new File(baseDir + "/" + absFileName);
            } else {
                ret = new File(baseDir + absFileName);
            }

            return ret;
        }
    }
}
